//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jim.server.command.handler;

import org.apache.commons.lang3.StringUtils;
import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.ImStatus;
import org.jim.core.config.ImConfig.Global;
import org.jim.core.exception.ImException;
import org.jim.core.message.MessageHelper;
import org.jim.core.packets.Command;
import org.jim.core.packets.MessageReqBody;
import org.jim.core.packets.RespBody;
import org.jim.core.packets.UserMessageData;
import org.jim.core.utils.JsonKit;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.protocol.ProtocolManager.Converter;

public class MessageReqHandler extends AbstractCmdHandler {
	public MessageReqHandler() {
	}

	public Command command() {
		return Command.COMMAND_GET_MESSAGE_REQ;
	}

	public ImPacket handler(ImPacket packet, ImChannelContext imChannelContext) throws ImException {
		MessageReqBody messageReqBody;
		try {
			messageReqBody = (MessageReqBody)JsonKit.toBean(packet.getBody(), MessageReqBody.class);
		} catch (Exception var16) {
			return this.getMessageFailedPacket(imChannelContext);
		}

		UserMessageData messageData = null;
		ImServerConfig imServerConfig = (ImServerConfig)Global.get();
		MessageHelper messageHelper = imServerConfig.getMessageHelper();
		String groupId = messageReqBody.getGroupId();
		String userId = messageReqBody.getUserId();
		String fromUserId = messageReqBody.getFromUserId();
		Double beginTime = messageReqBody.getBeginTime();
		Double endTime = messageReqBody.getEndTime();
		Integer offset = messageReqBody.getOffset();
		Integer count = messageReqBody.getCount();
		int type = messageReqBody.getType();
		if (!StringUtils.isEmpty(userId) && (0 == type || 1 == type) && ImServerConfig.ON.equals(imServerConfig.getIsStore())) {
			RespBody resPacket;
			if (type == 0) {
				resPacket = new RespBody(Command.COMMAND_GET_MESSAGE_RESP, ImStatus.C10016);
			} else {
				resPacket = new RespBody(Command.COMMAND_GET_MESSAGE_RESP, ImStatus.C10018);
			}

			if (!StringUtils.isEmpty(groupId)) {
				if (0 == type) {
					messageData = messageHelper.getGroupOfflineMessage(userId, groupId);
				} else if (1 == type) {
					messageData = messageHelper.getGroupHistoryMessage(userId, groupId, beginTime, endTime, offset, count);
				}
			} else if (StringUtils.isEmpty(fromUserId)) {
				if (0 != type) {
					return this.getMessageFailedPacket(imChannelContext);
				}

				messageData = messageHelper.getUserOfflineMessage(userId);
			} else if (0 == type) {
				messageData = messageHelper.getFriendOfflineMessage(userId, fromUserId);
			} else if (1 == type) {
				messageData = messageHelper.getFriendHistoryMessage(userId, fromUserId, beginTime, endTime, offset, count);
			}

			messageData.setType(type);
			resPacket.setData(messageData);
			return Converter.respPacket(resPacket, imChannelContext);
		} else {
			return this.getMessageFailedPacket(imChannelContext);
		}
	}

	public ImPacket getMessageFailedPacket(ImChannelContext imChannelContext) throws ImException {
		RespBody resPacket = new RespBody(Command.COMMAND_GET_MESSAGE_RESP, ImStatus.C10015);
		return Converter.respPacket(resPacket, imChannelContext);
	}
}
